import { createVNode, App, render } from 'vue'
import PageLoading from '@src/components/Loading/index.vue'
import Progress from '@src/components/Progress/index.vue'
const PageLoadingVn = createVNode(PageLoading)
const ProgressVn = createVNode(Progress)
export default {
  install(app: App<any>): any {
    app.provide('progress', () => {
      let el = document.querySelector('#progress')
      if (el) {
        render(ProgressVn, el)
      } else {
        if (ProgressVn.el) {
          if (el && (el as Element)) {
            const e = el as any
            e.removeChild(ProgressVn.el)
          }
        }
      }
    })
    app.directive('y-loading', {
      mounted(el, binding) {
        if (binding.value) {
          render(PageLoadingVn, el)
        } else {
          if (PageLoadingVn.el) {
            el.removeChild(PageLoadingVn.el)
          }
        }
      },
    })
  },
}
